package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"z-blog/database"
	"z-blog/models"
)

var H = make(map[string]interface{})

//校验token
func auth(c *gin.Context) {
	user := c.GetHeader("username")
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  user,
	})
}

//user
func login(c *gin.Context) {
	var data models.WebLoginModel
	_ = c.ShouldBindJSON(&data)
	//校验用户密码正确性
	res, token := database.UserLogin(data.Username, data.Password)
	if res {
		c.JSON(http.StatusOK, gin.H{
			"code":  200,
			"token": token,
			"msg":   "登陆成功",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "登录失败",
		})
	}
}

//获取文章内容
func getArticleDetails(c *gin.Context) {
	var getArticleDetailsModel models.GetArticleDetailsModel
	_ = c.ShouldBindJSON(&getArticleDetailsModel)
	id, _ := strconv.Atoi(getArticleDetailsModel.ID)
	res := database.GetArticleDetails(id)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  res,
	})
}

//admin
func addSort(c *gin.Context) {
	var addSortModel models.AddSortModel
	_ = c.ShouldBindJSON(&addSortModel)
	res := database.AddSort(addSortModel.UrlName, addSortModel.Name)
	if res {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
	}
}

func getSorts(c *gin.Context) {
	//获取全部分类列表
	data := database.GetSorts()
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  data,
	})
}

func deleteSort(c *gin.Context) {
	var deleteSortModel models.DeleteSortModel
	_ = c.ShouldBindJSON(&deleteSortModel)
	sorts := database.DeleteSort(deleteSortModel.SortName)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  sorts,
	})
}

//保存排序规则
func sortMenu(c *gin.Context) {
	var sortMenuModel models.SortMenuModel
	_ = c.ShouldBindJSON(&sortMenuModel)
	sorts := database.SortMenu(sortMenuModel.Data)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  sorts,
	})
}

func releaseArticle(c *gin.Context) {
	var articleContentModel models.ArticleContentModel
	_ = c.ShouldBindJSON(&articleContentModel)
	user := c.GetHeader("username")
	err := database.ReleaseArticle(articleContentModel, user)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  err,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "创建成功",
		})
	}
}

func getArticleList(c *gin.Context) {
	zero := H
	ArticleList := database.GetArticleList()
	zero["total"] = len(ArticleList)
	if len(ArticleList) > 10 {
		zero["data"] = ArticleList[0:10]
	} else {
		zero["data"] = ArticleList
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  zero,
	})
}

func pagingFind(c *gin.Context) {
	var pagingFindModel models.PagingFindModel
	_ = c.ShouldBindJSON(&pagingFindModel)
	data, total := database.PagingFind(pagingFindModel)
	zero := H
	zero["data"] = data
	zero["total"] = total
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  zero,
	})

}

func getClassifyList(c *gin.Context) {
	var classifyListModel models.ClassifyListModel
	_ = c.ShouldBindJSON(&classifyListModel)
	data := database.GetClassifyList(classifyListModel)
	zero := H
	if len(data) <= 10 {
		zero["data"] = data
		zero["total"] = len(data)
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  zero,
		})
	} else {
		zero["data"] = data[1:10]
		zero["total"] = len(data)
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  zero,
		})
	}
}

func getClassifyPaging(c *gin.Context) {
	var getClassifyPagingModel models.GetClassifyPagingModel
	_ = c.ShouldBindJSON(&getClassifyPagingModel)
	res, total := database.GetClassifyPaging(getClassifyPagingModel)
	zero := H
	zero["data"] = res
	zero["total"] = total
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  zero,
	})
}

func deleteArticle(c *gin.Context) {
	var deleteArticleModel models.DeleteArticleModel
	_ = c.ShouldBindJSON(&deleteArticleModel)
	database.DeleteArticle(deleteArticleModel)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}

func findArticleInfo(c *gin.Context) {
	var findArticleInfoModel models.FindArticleInfo
	_ = c.ShouldBindJSON(&findArticleInfoModel)
	content := database.FindArticleInfo(findArticleInfoModel.ID, findArticleInfoModel.Title)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  content,
	})
}

func updateArticle(c *gin.Context) {
	var updateArticleModel models.UpdateArticleModel
	_ = c.ShouldBindJSON(&updateArticleModel)
	err := database.UpdateArticle(updateArticleModel)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  err,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "更新成功",
		})
	}
}

func searchArticle(c *gin.Context) {
	var searchArticleModel models.SearchArticleModel
	_ = c.ShouldBindJSON(&searchArticleModel)
	conList := database.SearchArticle(searchArticleModel.Content)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  conList,
	})
}
